﻿using System;
using System.Web.UI.WebControls.WebParts;
using System.Runtime.InteropServices;
using Microsoft.TeamFoundation.SharePoint;

using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebPartPages;
using Microsoft.SharePoint;

using System.Web;

namespace Portal.Source.WebParts
{
    using ScrumForTeamSystem.WebControls.Common;

    [Guid("E335464B-D777-42cb-86DF-442F187AAD5A")]
    public class ReportViewer : System.Web.UI.WebControls.WebParts.WebPart
    {
        private PageViewerWebPart _pageViewer;
        private ErrorDisplay2 _errorControl;
        private Label _urlForDebug;
        
        private string _reportPath;
        private string _reportParameters;

        [WebPartStorage(Storage.Shared)]
        [WebBrowsable(true)]
        [Personalizable(true)]
        public string ReportParameters
        {
            get { return _reportParameters; }
            set { _reportParameters = value; }
        }

        [WebPartStorage(Storage.Shared)]
        [WebBrowsable(true)]
        [Personalizable(true)]
        public string ReportPath
        {
            get { return _reportPath; }
            set
            {
                _reportPath = value;
            }
        }
 
        /// <summary>
        /// 
        /// </summary>
        protected override void CreateChildControls()
        {            
            base.CreateChildControls();
            _pageViewer = new PageViewerWebPart();
            _pageViewer.ChromeType = PartChromeType.None;
            _errorControl = new ErrorDisplay2();
            _urlForDebug = new Label();            
            Controls.Add(_pageViewer);
            Controls.Add(_errorControl);
            Controls.Add(_urlForDebug);
        }

        /// <summary>
        /// Render the report link requested from the properties
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            try
            {
                _errorControl.ErrorMessage = string.Empty;

                ProjectInfo info = TeamFoundationWeb.GetProject(SPContext.Current.Web);
                ReportingConfigurationInfo reportInfo = info.ReportingConfiguration;

                // If no report path then don't render
                if (!string.IsNullOrEmpty((_reportPath)))
                {
                    string url = reportInfo.ReportWebServiceUrl + "/Pages/ReportViewer.aspx?" +
                                 HttpContext.Current.Server.UrlEncode(_reportPath) +
                                 "&rs:Command=Render&rs:format=HTML4.0&rc:Toolbar=false";

                    if (_reportParameters != null)
                    {
                        string[] paramArray = _reportParameters.Split(';');
                        
                        for(int i = 0;i < paramArray.Length;i++)
                        {
                            string[] keyValue = paramArray[i].Split('=');

                            if (keyValue.Length == 2)
                            {
                                url += "&" + keyValue[0] + "=" + keyValue[1];
                            }
                        }
                    }
                    _urlForDebug.Text = url;
                    _pageViewer.ContentLink = url;
                }
            }
            catch (Exception ex)
            {
                _errorControl.ErrorMessage = ex.Message;
            }
            
            base.OnPreRender(e);
        }
    }
}
